<?php

namespace app\apicompany\controller;

use app\common\controller\Api;
use think\Config;
use think\Exception;

/**
 * 企业招聘信息表
 */
class EnterpriseInvite extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    protected $failException = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\apicompany\model\Invite();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }


    /**
     * 列表
     * @return void
     */
    public function list()
    {
        $input = input();
        try {
            $pageArr = [
                'page' => 1,
                'page_limit' => 30,
            ];
            $where['user_id'] = $this->auth->getUser()['id'];
            if (!empty($input['id'])) {
                $where['m.id'] = $input['id'];
            }
            if (!empty($input['keyWork'])) {
                $where['m.name'] = ['like', '%' . $input['keyWork'] . '%'];
            }
            if (!empty($input['personage_post_id'])) {
                $where['personage_post_id'] = $input['personage_post_id'];
            }
            if (!empty($input['province'])) {
                $where['province'] = $input['province'];
            }

            if (!empty($input['city'])) {
                $where['city'] = $input['city'];
            }
            if (!empty($input['district'])) {
                $where['district'] = $input['district'];
            }
            $pageArr = array_merge($pageArr, $input);
            $data = $this->model->alias('m')
                ->where($where)
                ->order('createtime desc')
                ->paginate([
                    'page' => $pageArr['page'] ?: 1,
                    'list_rows' => $pageArr['page_limit'] ?: 10
                ]);

            $this->success('ok', $data);
        }catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }


    /**
     * 添加
     * @return void
     */
    public function add(){
        $input = input('post.');
    

        try {
            if(!empty($input['id'])){
                $id = $input['id'];
                unset($input['id']);
                $this->model->where('id',$id)->update($input);
            }else {
                $input['user_id'] = $this->auth->getUser()['id'];
                $input['createtime'] = time();
                $this->model->insert($input);
            }

            $this->success('保存成功');
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }


    /**
     * 删除
     * @return void
     */
    public function del(){
        $input = input('post.');
        try {
            $this->model->where('id',$input['id'])
                ->where('user_id',$this->auth->getUser()['id'])
                ->delete();
            $this->success('ok');
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }
}